function main() {

    const self = this;
    self.isLoading = true;
    // 获取表单组件
    var formPartEle = self.$$root().$$("form_part_87u0zh");
    var huamnId = formPartEle.$$getRenderElement().getDocumentNo();

    if (formPartEle) {

        if (huamnId) {
            // 编辑
            saveHuman(self, formPartEle).then(res => {
                debugger
                if (res.result) {
                    // formPartEle.$$getRenderElement().$$setExtraParams({
                    //     "documentNo": huamnId
                    // });
                    self.isLoading = false;
                }
            }).catch(err => {
                console.log(err);
                self.isLoading = false;
            });

        } else {
            // 判断是否需要审核，读取配置表信息
            api.EXEC_FORMULA("ENTITY_SELECT_ONE('t_4916fdfaf0174ad19ff7e4ed5355c90a',enable_flag,QUERY_EQ(id,1))").then(res => {
                if (res.result === "1") {
                    // 设置审核状态为待审核
                    // 默认设置为待入职
                    const setValues = [
                        self.$$("number_input_d0ez1e").$$setValue("0"),
                        self.$$("single_select_f97zsd").$$setValue("5")
                    ];
                    Promise.all(setValues).then(() => {
                        saveHuman(self, formPartEle).then(res => {
                            if (res.result) {

                                let humanUUID = res.result["t_2488d76f089b4f00aae44062307659fd"].text_input_1cr5k1;
                                let documentNo = res.result.documentNo;


                                insertApproval(humanUUID);
                                const syncHuman = [{
                                    name: "id",
                                    valueContent: documentNo
                                }];
                                syncHumanToUserCenter(syncHuman).then(res=>{
                                    formPartEle.$$getRenderElement().$$setExtraParams({
                                        "documentNo": documentNo
                                    });
                                })

                                self.isLoading = false;
                            }
                            self.isLoading = false;
                        }).catch((error) => {
                            console.log(error);
                            self.isLoading = false;
                        });
                    })
                        .catch((error) => {
                            console.log(error);
                            self.isLoading = false;
                        });

                } else {

                    const setValues = [
                        self.$$("number_input_d0ez1e").$$setValue("1"),
                        self.$$("single_select_f97zsd").$$setValue("1")
                    ];
                    Promise.all(setValues)
                        .then(() => {
                            saveHuman(self, formPartEle).then(res => {
                                console.log(res);
                                self.isLoading = false;
                            }).catch((error) => {
                                console.log(error);
                                self.isLoading = false;
                            });
                        }).catch(error => {
                        self.isLoading = false;
                        console.log(error);
                    });

                }
            });
        }
    }
}


function saveUserName(self) {

    return new Promise((resolve, reject) => {
        // 获取当前新增人员姓名
        let humanName = self.$$("text_input_cfmyq9").$$getValue();
        const params = [{
            name: "humanName",
            valueContent: humanName
        }];

        // 判断新增人员是否重名
        //"be299e8c-451f-485c-90f9-fd95fe1ffa39"


        // 不重名赋值 用户名称 text_input_mz1u9 为当前新增人员姓名
        // 重名赋值 用户名称 text_input_mz1u9 为当前新增人员姓名+联系电话（text_input_cfmyq9    text_input_h7gdpb）

        getApiValue(params).then(res => {
            console.log(res);
            let userName = "";
            let identityNo = "";
            if (parseInt(res.count) > 0) {
                identityNo = self.$$("text_input_8tpgxe").$$getValue();
                if (identityNo) {
                    userName = humanName + identityNo.slice(-4);
                }
            } else {
                userName = humanName;
            }
            resolve(userName)

        });

    });

}


function syncHumanToUserCenter(params) {
    return new Promise((resolve, reject) => {
        api.DATA_SOURCE_EXECUTE("ds_api:7a33e6eb-1ea6-46d3-be49-afca3a903a58", "api", params, [], "")
            .then(res => {
                try {
                    const resValue = JSON.parse(res.result);
                    console.log(resValue);
                    resolve(resValue);
                } catch (parseError) {
                    console.error("解析API响应时出错:", parseError);
                    reject(parseError);
                }
            })
            .catch(apiError => {
                console.error("调用API时出错:", apiError);
                reject(apiError);
            });
    });
}


function getApiValue(params) {
    return new Promise((resolve, reject) => {
        api.DATA_SOURCE_EXECUTE("be299e8c-451f-485c-90f9-fd95fe1ffa39", "api", params, [], "")
            .then(res => {
                try {
                    const resValue = JSON.parse(res.result);
                    console.log(resValue);
                    resolve(resValue);
                } catch (parseError) {
                    console.error("解析API响应时出错:", parseError);
                    reject(parseError);
                }
            })
            .catch(apiError => {
                console.error("调用API时出错:", apiError);
                reject(apiError);
            });
    });
}



/**
 * 保存人员信息
 */
function saveHuman(self, formPartEle) {

    return new Promise((resolve, reject) => {
        saveUserName(self).then(userName => {

            self.$$("text_input_b55o92").$$setValue(userName);

            formPartEle.$$getRenderElement().onSave().then((res) => {
                let documentNo = res.result.documentNo;
                formPartEle.$$getRenderElement().$$setExtraParams({
                    "documentNo": documentNo
                });
                // formPartEle.$$getRenderElement().$$setExtraParams({
                //     "documentNo": -1
                // }).then(()=>{
                //     resolve(res);
                // });
                resolve(res);

            }).catch(err => {
                reject(err);

            });
        });

    });
}

/**
 * 新增人员审核纪录
 */
function insertApproval(humanUUID) {

    let insertApprovalSql = `INSERT('t_655b23a930504145bf2ba2f014780dc6',['human_uuid','${humanUUID}','approval_type_code',1,'approval_type_name','人员新增','create_human_id',USER_ID(),'create_time',NOW()])`

    console.log(insertApprovalSql);

    api.EXEC_FORMULA(insertApprovalSql).then((res) => {
        console.log("新增人员审核成功", res);
    }).catch(err => {
        console.log(err);
    });
}

